IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_UserGetByEmail]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_UserGetByEmail]
GO

CREATE PROCEDURE cms_sp_UserGetByEmail
	@Email nvarchar(250),
	@SiteId int,
	@SiteGroupId int
AS
BEGIN
	select 
	Users.UserId,
	Users.SiteId,
	Users.SiteGroupId,
	Users.LoginName,
	Users.UserEmail,
	Users.Firstname,
	Users.Lastname,
	Users.IsEnabled,
	Users.IsSystem,
	Users.CreatedBy,
	Users.DateCreated,
	Users.LastUpdatedBy,
	Users.DateLastUpdated
	from [dbo].[Users]
	WHERE Users.UserEmail = @Email and 
	(([SiteGroupId] is null and [SiteId] is null) or 
	([SiteGroupId] = @SiteGroupId and [SiteId] is null) or
	([SiteGroupId] = @SiteGroupId and [SiteId] = @SiteId))	
END
GO